﻿using System.Windows.Controls;

namespace ObjectEditor.Validation
{
    public class LongRule : ValidationRule
    {
        public long Max { get; set; }
        public long Min { get; set; }

        public LongRule()
        {
            Min = long.MinValue;
            Max = long.MaxValue;
        }

        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            long longValue = 0;
            try
            {
                if (!string.IsNullOrWhiteSpace(value.ToString()))
                {
                    longValue = long.Parse(value.ToString(), cultureInfo);
                }
            }
            catch
            {
                return new ValidationResult(false, "not correct value");
            }
            if (longValue < Min || longValue > Max)
            {
                return new ValidationResult(false, string.Format("value is not in range [{0}..{1}]", Min, Max));
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }
    }
}
